import {
	Modal,
	message,
	Button,
} from 'antd';
import {
	WeaMoreButton,
	WeaLocaleProvider,
} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const confirm = Modal.confirm;

const fetch = (params) => {
	const {
		asyncFetch,
		name,
		fetchParams = {},
		logic,
	} = params;

	asyncFetch[name](fetchParams).then(cb => {
		const {
			api_status,
			status,
		} = cb;

		if (api_status || status === '1') {
			logic(cb);
		} else {
			message.error();
		}
	}).catch(error => {
		message.error();
	});
}

const getDialogButtons = (params) => {
	const {
		datas,
		dropMenuDatas = [],
	} = params;

	const btns = [];
	datas.map((data, index) => {
		const {
			name,
			disabled,
			onBtnClick
		} = data;
		btns.push(
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@6lg0of@${diffKindexey}`} type="primary" disabled={disabled} onClick={onBtnClick}>{name}</Button>
		);
	});
	btns.push(
		<WeaMoreButton ecId={`${this && this.props && this.props.ecId || ''}_WeaMoreButton@qupv6k@${index}`} datas={dropMenuDatas}/>
	);
	return btns
}

const getMenusCreationConfig = (datas) => {
	const topMenu = datas.filter(item => {
		const {
			isTop,
			isBatch
		} = item;
		return isTop === '1' || isBatch === '1'
	})

	const rightMenu = datas;

	return {
		topMenu,
		rightMenu
	}
}

function getTopButtons(params) {
	const {
		datas,
		disabled,
	} = params;

	let btns = [];
	datas.map((item, index) => {
		const {
			menuFun,
			menuName
		} = item;

		btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@4dlu38@${index}`} type="primary" disabled={Array.isArray(disabled) ? disabled[index] : disabled } onClick={this[menuFun]}>{menuName}</Button>)
	});

	return btns
}

function getDropMenuDatas(params) {
	const {
		datas,
		disabled,
	} = params;

	let dropMenuDatas = [];

	datas.map((item, index) => {
		const {
			menuFun,
			menuName,
			menuIcon
		} = item;

		dropMenuDatas.push({
			key: index.toString(),
			disabled: Array.isArray(disabled) ? disabled[index] : disabled,
			icon: <i className={`${menuIcon}`} />,
			content: menuName,
			onClick: this[menuFun]
		});
	});

	return dropMenuDatas;
}

const getSearchButtons = (params) => {
	const {
		form,
		search,
		cancel,
	} = params;

	const buttons = [];
	const buttonNames = [getLabel(82529, '搜索'), getLabel(27088, '重置'), getLabel(32694, '取消')];

	[0, 1, 2].map((v, index) => {
		buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@w2thaa@${index}`} type={v===0 ? "primary" : ''} onClick={() => {v===0 ? search() : v=== 1 ? form.reset() : cancel()}}>{buttonNames[v]}</Button>);
	});

	return buttons;
}


const showFailMsg = (messages, ratio) => {
	const msgs = [],
		duration = messages.length;

	messages.map((msg, index) => msgs.push(<li>{`${index + 1}. ${msg}`}</li>));

	message.warning(<ul>{msgs}</ul>, duration * ratio);
}

const msg = {
	showFailMsg: showFailMsg
}

const isComplete = (fields, datas) => {
	const unfilledRows = [];

	datas.map((data, index) => {
		fields.some(field => data[field] === undefined) && unfilledRows.push(index + 1);
	});

	(unfilledRows.length > 0) && message.warning(`${getLabel('15323',"第")} ${unfilledRows.toString()} ${getLabel('27592',"行")}${getLabel('389070',"余额发放")}${getLabel('496',"天数")}${getLabel('21423',"未填写")}`);

	if (unfilledRows.length > 0) return false;

	return true;
}

const validate = {
	isComplete: isComplete
}

const add = (config) => {
	const {
		fields,
		intervals,
		datas,
		msgs
	} = config;

	const last = datas[datas.length - 1];
	const clone = [...datas];

	if (!validate.isComplete(fields, datas)) return datas;

	const newData = {};

	Object.keys(last).map((key, index) => {
		if (!fields.includes(key)) {
			Object.assign(newData, {
				[key]: last[key] + intervals[key],
			});
		} else {
			Object.assign(newData, {
				[key]: last[key] + 1,
			});
		}
	});

	clone.push(newData);

	return clone;

}

const rule = {
	add: add
}

const getArrayDatas = (cb) => {
	let datas = [];

	const keys = Object.keys(cb);

	keys.map(key => {
		if (Array.isArray(cb[key])) {
			datas = cb[key];
		}
	});

	return datas;
}

const getTabId = (cb) => {
	let ids = [];

	const keys = Object.keys(cb);

	keys.map(key => {
		if (Array.isArray(cb[key])) {
			cb[key].map(item => {
				item.id && ids.push(item.id);
			});
		}
	});

	return ids
}

const callback = {
	getArrayDatas: getArrayDatas,
	getTabId: getTabId,
}

const componentReset = {
	resetRadioGroup: (ref) => {
		ref.resetDefault();
	},
	resetBrowser: (ref) => {
		ref.set([])
	}
}

const capitalize = ([first, ...rest], lowerRest = false) => {
	return first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
}

const string = {
	capitalize: capitalize
}

const executeFunction = (config) => {
	const {
		functions,
		params,
		father
	} = config;

	functions.map((func, index) => {
		const object = Array.isArray(father) ? father[index] : father;
		object[func](params ? params[index] : '');
	});
}

const page = {
	initConfig: (params, pointer) => {
		Object.keys(params).map(key => {
			pointer[key] = params[key];
		});
	}
}

const isUnique = (val, arr) => {
	let flag = true;

	arr.map(ele => {
		if (ele === val) {
			flag = false;
		}
	});

	return flag;
}

const array = {
	isUnique: isUnique
}

const showDelConfirm = (params) => {
	const {
		type,
		logic
	} = params;

	confirm({
		title: getLabel('131329', '信息确认'),
		content: (type === 0) ? getLabel('83877', '确定要删除吗？') : getLabel('385625', '确定要删除选择的记录吗？'),
		okText: getLabel('33703', '确定'),
		cancelText: getLabel('32694', '取消'),
		onOk() {
			logic(type);
		},
		onCancel() {
			return false;
		},
	});
}

const confirmation = {
	showDelConfirm: showDelConfirm
}

const exportExcel = (params, downloadUrl) => {
	let formElement = document.createElement('form');
	formElement.style.display = "display:none;";
	formElement.method = 'post';
	formElement.action = downloadUrl;
	let inputElement;
	[...Object.keys(params)].map(k => {
		inputElement = document.createElement('input');
		inputElement.type = 'hidden';
		inputElement.name = k;
		inputElement.value = params[k];
		formElement.appendChild(inputElement);
	});
	inputElement = document.createElement('input');
	inputElement.type = 'hidden';
	inputElement.name = 'reportType';
	inputElement.value = 'kqReport';
	formElement.appendChild(inputElement);
	document.body.appendChild(formElement);
	formElement.submit();
	document.body.removeChild(formElement);
}

export {
	fetch,
	getDialogButtons,
	getMenusCreationConfig,
	getTopButtons,
	getDropMenuDatas,
	getSearchButtons,
	msg,
	validate,
	rule,
	callback,
	componentReset,
	string,
	executeFunction,
	page,
	array,
	confirmation,
	exportExcel,
}